package manual_confirm

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	xerrors "gitee.com/liuxuezhan/ar-platform/admin-api/pkg/xerrors/assets"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ManualConfirmLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 财务管理--确认补单
func NewManualConfirmLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ManualConfirmLogic {
	return &ManualConfirmLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ManualConfirmLogic) ManualConfirm(req *types.ManualConfirmReq) (resp *types.ManualConfirmResp, err error) {
	if req.OrderStatus != "1" && req.OrderStatus != "2" {
		return nil, xerrors.ErrManualConfirmOrderStatus
	}
	adminId := headInfo.GetTokenUid(l.ctx)
	if req.OrderStatus == "1" {
		if _, err = l.svcCtx.AssetsRpc.PayRecharge.RechargeSuccess(l.ctx, &rpc.NotifyOKRequest{
			OrderNo:    req.OrderSn,
			ReturnJson: "Administrator confirms supplementary order [admin_id: " + strconv.Itoa(int(adminId)) + "]",
		}); err != nil {
			return nil, err
		}
	}
	if req.OrderStatus == "2" {
		if _, err = l.svcCtx.AssetsRpc.PayRecharge.RechargeFail(l.ctx, &rpc.NotifyFailRequest{
			OrderNo:    req.OrderSn,
			ReturnJson: "Administrator cancel order [admin_id: " + strconv.Itoa(int(adminId)) + "]",
		}); err != nil {
			return nil, err
		}
	}
	return
}
